package com.freshcity.page.tree
{
	import com.freshcity.IPresentWater;
	import com.freshcity.component.container.Component;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.tree.GiveWaterWin;
	import com.freshcity.tree.WaterItem;
	import com.freshcity.utils.MessageUtil;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class GiveWater extends Component
	{
		public var tree:IPresentWater;
		private var back:GiveWaterWin;
		private var waterBar:WaterBar;
		private var selectedWater:WaterItem;
		private var backMask:Sprite;
		
		public function GiveWater(xpos:Number=0, ypos:Number=0)
		{
			var parent:DisplayObjectContainer = LayerManager.msgTipLayer;
			super(parent, xpos, ypos);
			this.x = (stage.width-back.width)/2;
			this.y = (stage.height-back.height)/2;
			
			back.cancelBtn.addEventListener(MouseEvent.CLICK,hide);
			back.closeBtn.addEventListener(MouseEvent.CLICK,hide);
			back.addBtn.addEventListener(MouseEvent.CLICK,addBtnHandler);
			back.submitBtn.addEventListener(MouseEvent.CLICK,onSubmit);
			back.removeBtn.addEventListener(MouseEvent.CLICK,removeBtnClickHandler);
			this.waterBar.addEventListener(Event.SELECT,onWaterSelectHandler);
		}
		
		protected function removeBtnClickHandler(event:MouseEvent):void
		{
			main.commu.send(OpCodes.REMOVE_WATER,{index:selectedWater.index},removeWaterCallback);
		}
		
		private function removeWaterCallback(data:*):void
		{
			main.user.waterInfo = data;
			waterBar.setData(main.user.waterInfo);
			main.msg.showFixRollMsg("丢掉了 "+ MessageUtil.decodeWater(selectedWater.category));
			switchBtnStatus(false);
		}
		
		protected function onWaterSelectHandler(event:DataEvent):void
		{
			this.selectedWater = event.data as WaterItem;
			switchBtnStatus(true);
		}
		
		private function switchBtnStatus(flag:Boolean):void
		{
			back.submitBtn.enabled = flag;
			back.removeBtn.enabled = flag;
		}
		
		protected function onSubmit(event:MouseEvent):void
		{
			if(waterBar.selectedItem){
				tree.disableSubmitButton();
				//main.commu.send(OpCodes.REQ_PRESENT_WATER,{u:tree.data.owner,i:waterBar.selectedItem.index},onCallback);
				main.statusPanel.timeBar.data=48;
				main.statusPanel.timeBar.callBackFun = finishPresentTimeCallback;
				switchBtnStatus(false);
				//this.hide();
			}else{
				main.msg.showRollTipsMsg("请先选择一份清水");
			}
		}
		
		
		public function finishPresentTimeCallback():void
		{
			main.commu.send(OpCodes.PRESENT_WATER, {u:tree.data.owner,i:waterBar.selectedItem.index}, finishPresentCallBack);
		}
		
		private function finishPresentCallBack(data:*):void
		{
			if (data.isSuccess == false)
			{
				main.msg.showRollTipsMsg("对方存水已满");
			}
			else
			{
				var s:String="";
				s="成功赠送用户" + tree.data.owner + "一份" + MessageUtil.decodeWater(selectedWater.category);
				main.msg.showRollTipsMsg(s);
				main.msg.showFixRollMsg(s);
				main.user.waterInfo=data.myWaterInfo;
				waterBar.setData(main.user.waterInfo);
			}
			main.treesDict[data.owner]=data;
			tree.renderTree(data);
		}		
		
		override protected function addChildren():void
		{
			super.addChildren();
			back = new GiveWaterWin();
			addChild(back);
			
			waterBar=new WaterBar(back, 80, 50);
		}
		
		override public function destroy():void
		{
			back.cancelBtn.removeEventListener(MouseEvent.CLICK,hide);
			back.closeBtn.removeEventListener(MouseEvent.CLICK,hide);
			back.addBtn.removeEventListener(MouseEvent.CLICK,addBtnHandler);
			back.submitBtn.removeEventListener(MouseEvent.CLICK,onSubmit);
			back.removeBtn.removeEventListener(MouseEvent.CLICK,removeBtnClickHandler);
			this.waterBar.removeEventListener(Event.SELECT,onWaterSelectHandler);
			super.destroy();
		}
		
		private function addBtnHandler(event:Event):void
		{
			this.hide();
			MainScene.userInfoHome.visible=false;
			main.mainNav.onShowTree();
		}
		
		override public function show(event:Event=null):void
		{
			backMask= new Sprite();
			backMask.graphics.clear();
			backMask.graphics.beginFill(0xffffff, 0);
			backMask.graphics.drawRect(0, 0, stage.stageWidth,stage.stageHeight);
			backMask.graphics.endFill();
			parent.addChildAt(backMask,0);
			
			waterBar.main=this.main;
			waterBar.setData(main.user.waterInfo);
			waterBar.show();
			super.show(event);
		}
		
		override public function hide(event:Event=null):void
		{
			if(backMask.parent)backMask.parent.removeChild(backMask);
			super.hide(event);
		}
		
	}
}